package com.fzkj.nio.part1;

import java.nio.ByteBuffer;

public class Test {

    public static void main(String[] args) {
        // 1、分配缓冲区，设置容量为10
        ByteBuffer buffer = ByteBuffer.allocate(10);
        System.out.println(buffer.position()); // 0
        System.out.println(buffer.limit()); // 10
        System.out.println(buffer.capacity()); // 10
        System.out.println("--------------------");

        // 2、添加数据
        String name = "ymm";
        buffer.put(name.getBytes());
        System.out.println(buffer.position()); // 3
        System.out.println(buffer.limit()); // 10
        System.out.println(buffer.capacity()); // 10
        System.out.println("--------------------");

        // 3、将缓冲区的界限设置为当前位置，并将当前位置设置为0：切换为可读
        buffer.flip();
        System.out.println(buffer.position()); // 0
        System.out.println(buffer.limit()); // 3
        System.out.println(buffer.capacity()); // 10
        System.out.println("--------------------");

        // 4、读取
        char na = (char) buffer.get(1);
        System.out.println(na);
        System.out.println(buffer.position()); // 0
        System.out.println(buffer.limit()); // 3
        System.out.println(buffer.capacity()); // 10
        System.out.println("--------------------");

    }

}
